package com.emit.controller;

import com.emit.domain.Menu;
import com.emit.exception.BizException;
import com.emit.form.MenuForm;
import com.emit.service.MenuService;
import com.emit.view.ResultView;
import com.emit.vo.MenuVO;
import com.github.pagehelper.PageSerializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 菜单控制层
 *
 * @author zhangchao
 * @version 1.0.0
 * @date 2021-10-28
 * @menu 菜单
 */
@RestController
@RequestMapping(value = "/menu")
public class MenuController {

    @Resource
    private MenuService menuService;
    @Autowired
    private MessageSource messageSource;
    /**
     * 根据角色权限查询用户菜单
     */
    @PostMapping("/user/menus")
    public ResultView<List<MenuVO>> findUserMenus() {
        // todo 根据token获取用户id  暂时查询所有菜单
        List<MenuVO> userMenus = menuService.findUserMenusByUserId(1L);
        return new ResultView<>(userMenus);
    }

    /**
     * 分页展示
     * @param menuForm 表单数据
     * @return 分页数据
     */
    @PostMapping("/menus")
    public ResultView<PageSerializable<MenuVO>> findMenus(@RequestBody MenuForm menuForm) {
        PageSerializable<MenuVO> menus = menuService.findMenus(menuForm);
        return new ResultView<>(menus);
    }

    /**
     * 菜单下拉框
     * @return 菜单集合数据
     */
    @PostMapping("/options")
    public ResultView<List<Menu>> findMenuOptions() {
        List<Menu> menuList = menuService.findMenuOptions();
        List<Menu> menus = new ArrayList<>();

        //添加一个根数据
        Menu menu = new Menu();
        menu.setId(0L);
        menu.setName(messageSource.getMessage("menu.alldir",null, LocaleContextHolder.getLocale()));
        menu.setType(1);
        menus.add(menu);
        menus.addAll(menuList);
        return new ResultView<>(menus);
    }

    /**
     * 添加菜单
     * @param menuForm 菜单表单数据
     * @return 结果
     */
    @PostMapping("/add")
    @ResponseBody
    public ResultView<String> addMenus(@RequestBody MenuForm menuForm) {
        //非空检查
        if(menuForm==null){
            String message = messageSource.getMessage("error.B1001",null, LocaleContextHolder.getLocale());
            throw new BizException("B1001",message);
        }

        //添加数据
        if(menuService.addMenu(menuForm)){
            return new ResultView<>();
        }
        String message = messageSource.getMessage("error.B1002",null, LocaleContextHolder.getLocale());
        throw new BizException("B1002",message);
    }
    /**
     * 修改菜单
     */
    @PostMapping("/update")
    @ResponseBody
    public Object updateMenus(@RequestBody MenuForm menuForm) {
        // 非空检查
        if(menuForm==null){
            String message = messageSource.getMessage("error.B1001",null, LocaleContextHolder.getLocale());
            throw new BizException("B1001",message);
        }

        //修改数据
        if(menuService.updateMenu(menuForm)){
            return new ResultView<>();
        }
        String message = messageSource.getMessage("error.B1003",null, LocaleContextHolder.getLocale());
        throw new BizException("B1003",message);
    }

    /**
     * 删除菜单
     * @param menuForm 表单数据
     * @return 结果
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delMenus(@RequestBody MenuForm menuForm) {
        // 非空检查
        if(menuForm==null|| menuForm.getId()==null){
            String message = messageSource.getMessage("error.B1001",null, LocaleContextHolder.getLocale());
            throw new BizException("B1001",message);
        }

        // 删除数据
        if(menuService.deleteMenu(menuForm.getId())){
            return new ResultView<>();
        }
        String message = messageSource.getMessage("error.B1004",null, LocaleContextHolder.getLocale());
        throw new BizException("B1004",message);
    }

}
